package com.xbw.bookSale.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.*;
import com.xbw.bookSale.vo.*;

public class ModifyBookNumServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gbk");
		// PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String strId = request.getParameter("id");
		String strNum = request.getParameter("num");
		int id = 0, num = 0;
		if (strId != null && strNum != null && !strId.trim().equals("")
				&& !strNum.trim().equals("")) {
			try {
				id = Integer.parseInt(strId);
				num = Integer.parseInt(strNum);
			} catch (NumberFormatException e) {
				e.printStackTrace();
				return;
			}

			Map<Integer, CartItem> cart = (Map<Integer, CartItem>) session
					.getAttribute("CART");
			if (cart != null) {
				CartItem item = cart.get(id);
				if (item != null) {
					item.setNum(num);
				}
			}
		}
		response.sendRedirect("LookAtCart.jsp");
	}
}
